package com.hailan.bq;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author hailan
 * @version 1.0
 * @className Test
 * @description TODO
 * @date 2020-09-01 22:37
 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        //List
        //Set
//        test1();
//            test2();
//            test3();
//        test4();
        System.out.println(Runtime.getRuntime().availableProcessors());
    }


    /**抛出异常
     * @author hailan
     * @description //TODO
     * @date 23:04 2020/9/1 0001
     * @param
     * @return void
     **/
    public static void test1(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.add("c"));
        System.out.println("========================");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

    }

    /**有返回值，没有异常
     * @author hailan
     * @description //TODO
     * @date 23:05 2020/9/1 0001
     * @param
     * @return void
     **/
    public static void test2(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
//        System.out.println(blockingQueue.offer("d"));
        System.out.println("========================");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
    }

    /**等待 阻塞  一直阻塞
     * @author hailan
     * @description //TODO
     * @date 23:05 2020/9/1 0001
     * @param
     * @return void
     **/
    public static void test3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
    }


    /**等待 阻塞  超时等待
     * @author hailan
     * @description //TODO
     * @date 23:05 2020/9/1 0001
     * @param
     * @return void
     **/
    public static void test4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        blockingQueue.offer("a");
        blockingQueue.offer("b");
        blockingQueue.offer("c");
//        blockingQueue.offer("d",1, TimeUnit.SECONDS);
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
    }
}
